
/**   
*    
* 项目名称：a_yongan   
* 类名称：BusinessController   
* 类描述：   
* 创建人：liubo   
* 创建时间：2018年6月17日 上午11:05:17    
* 修改时间：2018年6月17日 上午11:05:17   
* 修改备注：   
* @version    
*    
*/
package com.yongan.business.web;

import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yongan.business.entity.Business;
import com.yongan.business.service.BusinessService;
import com.yongan.user.dto.UserResult;
import com.yongan.util.Execution;
import com.yongan.util.PageBean;
import com.yongan.util.SerialNumber;

/**
 * @author liubo
 *
 */
@Controller
@RequestMapping("/business")
public class BusinessController {

	@Autowired
	private BusinessService businessService;
	
	@RequestMapping(value = "/{type}/businesslist", method = RequestMethod.GET)
	public String deptcontent(@PathVariable("type") String type, Model model,HttpServletRequest request) {
		List<Business> businesslist = null;
		String page = request.getParameter("currentPage");
		int currentPage;
		int totalRecords;
		if (page != null) {
			currentPage = Integer.parseInt(page);
		} else {
			currentPage = 1;
		}
		PageBean<Business> pb = new PageBean<Business>();
		/**
		 * 0:客户
		 * 1：供应商
		 * 2：其他
		 */
		if(type.equals("0")){
			type = "客户";
		}else if(type.equals("1")){
			type = "供应商";
		}else{
			type = "其他";
		}
		totalRecords = businessService.getCountByType(type);
		pb.setCurrentPage(currentPage);
		pb.setTotalRecords(totalRecords);
		pb.setTotalPages(pb.getTotalRecords() % pb.getPagesize() == 0 ? pb
				.getTotalRecords() / pb.getPagesize() : pb.getTotalRecords()
				/ pb.getPagesize() + 1);
		if (totalRecords > 0) {
			int start = (pb.getCurrentPage() - 1) * pb.getPagesize();
			int end = pb.getCurrentPage() * pb.getPagesize();
			if (end > totalRecords - 1) {
				end = totalRecords;
			}
			businesslist = businessService.getBusinessList(start, end, type);
			//model.addAttribute("pb", pb);
		}
		model.addAttribute("pb", pb);
		model.addAttribute("businesslist", businesslist);
		model.addAttribute("businesstype", type);
		return "businesslist";
	}
	
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String businessadd(Model model) {
		//SerialNumber sn = new SerialNumber();
		String number = SerialNumber.Getnum();
		model.addAttribute("number", number);
		return "businessadd";
	}
	@RequestMapping(value = "/adddo", method = RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public UserResult<Execution> businessadddo(Business business, Model model) {
		business.setBusinessId(UUID.randomUUID().toString());
		Execution rs = businessService.executeBusiness(business, "", "1233213");
		int rsn = rs.getState();
		if (rsn == 1) {
			return new UserResult<Execution>(true, rs);
		}
		return new UserResult<Execution>(false, "");
	}
	
	@RequestMapping(value = "/{businessId}/detail", method = RequestMethod.GET)
	public String deptdetail(@PathVariable("businessId") String businessId, Model model) {
		if (!(businessId.equals("")|businessId==null)) {
			Business business = businessService.getBusinessById(businessId);
			model.addAttribute("business", business);
		}
		return "businessdetail";
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public UserResult<Execution> businessupdate(Business business, Model model) {
		Execution rs = businessService.executeBusiness(business, business.getBusinessId(), "123321231");
		int rsn = rs.getState();
		if (rsn == 1) {
			return new UserResult<Execution>(true, rs);
		}
		return new UserResult<Execution>(false, "");
	}
	
	@RequestMapping(value = "/{businessId}/delete", method = RequestMethod.GET, produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public UserResult<Execution> userDelete(
			@PathVariable("businessId") String businessId, Model model) {
		if (!(businessId.equals("")|businessId==null)) {
			Execution rsdelete = businessService.executeBusiness(null, businessId, "1212331232");
			int rsn = rsdelete.getState();
			if (rsn == 1) {
				return new UserResult<Execution>(true, rsdelete);
			}
		}
		return new UserResult<Execution>(false, "");
	}
}
